<?php
/**
 * Created by JetBrains PhpStorm.
 * User: GMC
 * Date: 10/3/13
 * Time: 2:20 PM
 * To change this template use File | Settings | File Templates.
 */

class WidgetExchange extends CWidget {
    public $exchange, $goldRates;

    public function init() {
        $exchange_cache = Yii::app()->cache->get('exchange_cache');
        $gold_cache = Yii::app()->cache->get('gold_cache');
        if ($gold_cache == null || count($gold_cache) == 0) {
            $Link = $Link2 = '';
            $dir = 'cache/';
            if (!is_dir($dir)) mkdir($dir, 0755, true);
            $Link = $dir . 'GoldRates.xml';
            $Link2 = 'http://www.sjc.com.vn/xml/tygiavang.xml'; // bảng tỷ giá vàng của sjc.com.vn
            $content = @file_get_contents($Link2);
            if ($content == '') {
                $content = @file_get_contents($Link);
            } else {
                copy($Link2, $Link);
            }
            $p = xml_parser_create();
            xml_parse_into_struct($p, $content, $xml);

            $goldrates['TPHCM'] = array(
                'id' => 'TPHCM'
            , 'name' => $xml[6]['attributes']['NAME']
            , 'buy' => $xml[7]['attributes']['BUY']
            , 'sell' => $xml[7]['attributes']['SELL']
            );

            Yii::app()->cache->set('gold_cache', $goldrates, Yii::app()->params['exchange_cache_time']);
            $gold_cache = Yii::app()->cache->get('gold_cache');
        }

        $this->goldRates = $gold_cache;

        if ($exchange_cache == null || count($exchange_cache) == 0) {
            $Link = $Link2 = '';
            $dir = 'cache/';
            if (!is_dir($dir)) mkdir($dir, 0755, true);
            $Link = $dir . 'ExchangeRates.xml';
            $Link2 = 'http://vietcombank.com.vn/ExchangeRates/ExrateXML.aspx'; // bảng tỷ giá ngoại tệ của vietcombank.com.vn
            $content = @file_get_contents($Link2);
            if ($content == '') {
                $content = @file_get_contents($Link);
            } else {
                copy($Link2, $Link);
            }

            $exchange_rates = array();
            if ($content != '' and preg_match_all('/Exrate CurrencyCode="(.*)" CurrencyName="(.*)" Buy="(.*)" Transfer="(.*)" Sell="(.*)"/', $content, $matches) and count($matches) > 0) {
                $exchange_rates = array(
                    'USD' => array()
                , 'EUR' => array()
                , 'GBP' => array()
                , 'HKD' => array()
                , 'JPY' => array()
                , 'CHF' => array()
                , 'AUD' => array()
                , 'CAD' => array()
                , 'SGD' => array()
                , 'THB' => array()
                );
                foreach ($matches[1] as $key => $value) {
                    if (isset($exchange_rates[$value])) {
                        $exchange_rates[$value] = array(
                            'id' => $value
                        , 'name' => $matches[2][$key]
                        , 'buy' => $matches[3][$key]
                        , 'transfer' => $matches[4][$key]
                        , 'sell' => $matches[5][$key]
                        );
                    }
                }
            }
            Yii::app()->cache->set('exchange_cache', $exchange_rates, Yii::app()->params['exchange_cache_time']);
            $exchange_cache = Yii::app()->cache->get('exchange_cache');
        }

        $this->exchange = $exchange_cache;
    }

    public function run() {
        return CWidget::render('widgetExchange', array('exchange_rates' => $this->exchange, 'gold_rates' => $this->goldRates));
    }
}